//------------------------------------------------------------------------------
//
//   Go4Flex 
//   Copyright 2010 
//   All rights reserved. 
//
//------------------------------------------------------------------------------

package com.demi.main.business
{
	import com.demi.main.event.DebugEvent;
	import com.demi.main.event.MainEvent;
	import com.demi.main.model.MainModel;
	import com.demi.main.view.LoginPopup;
	import com.demi.main.view.PreloaderPopup;
	import com.demi.main.vo.ConfigVO;
	import com.demi.main.vo.DebugVO;
	
	import flash.display.DisplayObject;
	import flash.events.IEventDispatcher;
	import flash.utils.getTimer;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	import mx.managers.PopUpManager;

	public class MainManager
	{
		public var dispatcher:IEventDispatcher;

		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------

		public var model:MainModel;
		private var loginPopup:LoginPopup;

		private var preloader:PreloaderPopup;

		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------

		public function addDebugInfo(info:DebugVO):void
		{
			if (!model.debugInfo)
			{
				model.debugInfo = new ArrayCollection([]);
			}
			model.debugInfo.source = ([info] as Array).concat(model.debugInfo.source);
		}

		public function hideLoginPopup():void
		{
			PopUpManager.removePopUp(loginPopup);
		}

		public function hidePreloaderPopup():void
		{
			PopUpManager.removePopUp(preloader);
		}

		public function loadExplorerModule():void
		{ 
			model.usercontrolModuleUrl = "module/usercontrol.swf";
			dispatcher.dispatchEvent(new DebugEvent(DebugEvent.ADD_INFO, new DebugVO("loading of usercontrol module")));
		}

		public function loginComplete(result:Boolean):void
		{
			if (result)
			{
				hideLoginPopup();
				var loadEvent:MainEvent = new MainEvent(MainEvent.LOAD_EXPLORER_MODULE);
				dispatcher.dispatchEvent(loadEvent);
				dispatcher.dispatchEvent(new DebugEvent(DebugEvent.ADD_INFO, new DebugVO("login successfully")));
			}
			else
			{
				setLoginStatus("Wrong login or password");
				dispatcher.dispatchEvent(new DebugEvent(DebugEvent.ADD_INFO, new DebugVO("login failed")));
			}
		}

		public function logoutComplete(result:Boolean):void
		{
			if (result)
			{
				model.usercontrolModuleUrl = null;
				//showLoginPopup();
			}
		}

		public function setConfigData(_data:XML):void
		{
			model.config = ConfigVO.parseXML(_data);
			dispatcher.dispatchEvent(new DebugEvent(DebugEvent.ADD_INFO, new DebugVO("config xml loaded")));
		}

		public function setLoadingStatus(s:String):void
		{
			preloader.loadingStatus = s;
		}

		public function setLoginStatus(_status:String):void
		{
			//loginPopup.loginStatus = _status;
		}

		public function showLoginPopup():void
		{
			loginPopup = new LoginPopup();
			loginPopup.dispatcher = dispatcher;
			PopUpManager.addPopUp(loginPopup, FlexGlobals.topLevelApplication as DisplayObject, true);
			PopUpManager.centerPopUp(loginPopup);
		}

		public function showPreloaderPopup():void
		{
			preloader = new PreloaderPopup();
			PopUpManager.addPopUp(preloader, FlexGlobals.topLevelApplication as DisplayObject, true);
			PopUpManager.centerPopUp(preloader);
		}
	}
}